﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using FluentValidation.Results;
using Microhard.Core;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.Msg;
using Microhard.Plat.CRM.Stores.Interface;


namespace Microhard.Plat.CRM.Stores.Validators
{
    public class CheckinModelValidator : AbstractValidator<CheckinModel>
    {
        public CheckinModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Error).Length(1, 250).WithMessage(CheckInMsg.MaxErrorLength).When(r => r.CheckID == Guid.Empty);
                RuleFor(u => u.Baidulat).GreaterThan(0).WithMessage(CheckInMsg.GetLatError).When(r => r.CheckID == Guid.Empty && string.IsNullOrEmpty(r.Error));
                RuleFor(u => u.Baidulat).GreaterThan(0).WithMessage(CheckInMsg.GetLngError).When(r => r.CheckID == Guid.Empty && string.IsNullOrEmpty(r.Error));
                Custom(u =>
                {
                    if (string.IsNullOrEmpty(u.Error))
                    {
                        if (u.SubKAsForCheckIn == null || u.SubKAsForCheckIn.Count == 0)
                        {
                            return new ValidationFailure("Address", CheckInMsg.OutRange);
                        }
                    }
                    ICheckin ici = IoC.Resolve<ICheckin>();
                    if (ici.IsDuplicateOnDay(u))
                    {
                        return new ValidationFailure("Address", CheckInMsg.Duplicate);
                    }
                    return null;
                });
            });
        }
    }
}
